﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.Input;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Text;

namespace VkSample81A
{
    public class TransformSystem : ComponentSystem
    {

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contextManager"></param>
        public TransformSystem()
        {
        }

        public override void Initialization()
        {
            ContextManager.Foreach<OBJGroup>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                IRenderObject render = context.GetContextRenderObject();
                var trans = render.GetComponent<Transform>(nameof(Transform));
                group.Transform.Position = trans.Position;
                group.Transform.Angle = trans.Rotation;
                group.Transform.Scale = trans.Scaling;
                return group;
            });
        }

        public override void Update(TimeSpan lastStamp)
        {
            ContextManager.Foreach<OBJGroup>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity);
                ref MVPMatrix mvp = ref context.GetMVPMatrix();
                mvp.ModelMatrix = Matrix4x4.CreateTranslation(group.Transform.Position) *
                Matrix4x4.CreateFromQuaternion(group.Transform.Angle) *
                Matrix4x4.CreateScale(group.Transform.Scale);
                context.PushToRenderGroup();
            });
        }
    }
}
